// import Cookies from 'js-cookie'
import sessionStorageUtil from '@/utils/sessionStorageUtil'
import localStorageUtil from '@/utils/localStorageUtil'

const TokenKey = 'Admin-Token'

export function getToken() {
  // Cookies.get(TokenKey)
  return sessionStorageUtil.fetch('Authorization')
}

export function setToken(token) {
  // Cookies.set(TokenKey, token)
  return sessionStorageUtil.save('Authorization', token)
}

export function removeToken() {
  // Cookies.remove(TokenKey)
  return sessionStorageUtil.delete('Authorization')
}

// 获取当前用户的所属组织
export function getOrganId() {
  return sessionStorageUtil.fetch('LOGIN_USER_ORGAN')
}

// 暂存当前用户的所属组织
export function setOrganId(organId) {
  return sessionStorageUtil.save('LOGIN_USER_ORGAN', organId)
}

// 暂存当前用户的所属组织
export function setTenantId(tenantId) {
  return sessionStorageUtil.save('LOGIN_USER_TENANT', tenantId)
}

// 暂存当前用户的所属组织
export function getTenantId() {
  return sessionStorageUtil.fetch('LOGIN_USER_TENANT')
}


// 获取当前用户的所属组织路径
export function getOrganPath() {
  return sessionStorageUtil.fetch('LOGIN_USER_ORGAN_PATH')
}

// 暂存当前用户的所属组织路径
export function setOrganPath(organPath) {
  return sessionStorageUtil.save('LOGIN_USER_ORGAN_PATH', organPath)
}

export function getLoginUser() {
  // Cookies.set(TokenKey, token)
  return sessionStorageUtil.fetch('USER_ID')
}

export function setLoginUser(userId) {
  // Cookies.set(TokenKey, token)
  return sessionStorageUtil.save('USER_ID', userId)
}


export function getDictionary() {
  return sessionStorageUtil.fetchObject('DICTINARY_LIST')
}

export function setDictionary(dictionaryList) {
  // Cookies.set(TokenKey, token)
  return sessionStorageUtil.saveObject('DICTINARY_LIST', dictionaryList)
}

export function getTenantList() {
  return sessionStorageUtil.fetchObject('TENANT_LIST')
}

export function setTenantList(tenantList) {
  // Cookies.set(TokenKey, token)
  return sessionStorageUtil.saveObject('TENANT_LIST', tenantList)
}

export function getOrganList() {
  return sessionStorageUtil.fetchObject('ORGAN_LIST')
}

export function setOrganList(organList) {
  // Cookies.set(TokenKey, token)
  return sessionStorageUtil.saveObject('ORGAN_LIST', organList)
}


//生成组织选项
export function createOrganOption() {

  let organList = getOrganList()
  let organMapping = {}
  if (organList.length) {
    for (let i = 0; i < organList.length; i++) {
      organMapping[organList[i].id] = organList[i].organName
    }
    localStorageUtil.save('organ_mapping', organMapping)
  }
}

// 翻译组织编号
export function translateOrganId2OrganName(row) {
  let organMapping = localStorageUtil.fetch('organ_mapping')
  return (organMapping && organMapping[row.organId]) || ''
}

// 过滤出系统错误
export function filterSystemErrorCode(dictionaryList) {

  const errorCodeFilter = "ERROR_CODE"
  const errorCodeItem = []
  if (dictionaryList || dictionaryList.length) {
    dictionaryList.forEach(function(element) {
      if (element.nameSpace === errorCodeFilter) {
        errorCodeItem.push(element)
      }
    })
    const errorCodeMap = {}
    errorCodeItem.forEach(function(element) {
      if (element.nameSpace === errorCodeFilter) {
        errorCodeMap[element.value] = element.desc
      }
    })
    localStorageUtil.save('error_map', errorCodeMap)
  }
}


// 获取系统错误
export function getSystemErrorCode(code) {
  let errorMap = localStorageUtil.fetch('error_map')
  if (errorMap[code]) {
    return errorMap[code]
  }
  return '系统错误'
}
